package com.ruoyi.web.controller.snmanagement;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.snmanagement.domain.PrinterSn;
import com.ruoyi.snmanagement.service.IPrinterSnService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/snmanagement/printerSn")
public class PrinterSnController extends BaseController {

    @Autowired
    private IPrinterSnService printerSnService;

    // 有条件的分页查询
    @GetMapping("/list")
    public TableDataInfo list(PrinterSn printerSn) {
        startPage();
        List<PrinterSn> list = printerSnService.getPrinterSnList(printerSn);
        return getDataTable(list);
    }


//    新增
    @PostMapping
    public AjaxResult add(@RequestBody PrinterSn printerSn) {
        int rows = printerSnService.insertPrinterSn(printerSn);
        return toAjax(rows);
    }

//    修改
    @PutMapping
    public AjaxResult update(@RequestBody PrinterSn printerSn) {
        int rows = printerSnService.updatePrinterSn(printerSn);
        return toAjax(rows);
    }

//删除
@DeleteMapping("/{id}")
public AjaxResult delete(@PathVariable Long id) {
    // 直接调用Service层的删除方法
    int rows = printerSnService.deletePrinterSn(id);
    return toAjax(rows);
}



//导出
@PostMapping("/export")
public void export(HttpServletResponse response, PrinterSn printerSn,
                   @RequestParam(value = "ids", required = false) String ids) {
    List<PrinterSn> list;
    if (ids != null && !ids.isEmpty()) {
        String[] idArray = ids.split(",");
        List<Long> idList = new ArrayList<>();
        for (String id : idArray) {
            idList.add(Long.valueOf(id));
        }
        list = printerSnService.getPrinterSnListByIds(idList);
        // 添加调试日志
        System.out.println("导出选中行数量: " + list.size());
        if (!list.isEmpty()) {
            System.out.println("第一条数据: " + list.get(0));
        }
    } else {
        list = printerSnService.getPrinterSnList(printerSn);
    }
    ExcelUtil<PrinterSn> util = new ExcelUtil<>(PrinterSn.class);
    util.exportExcel(response, list, "打印机序列号数据");
}





//     下载导入模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<PrinterSn> util = new ExcelUtil<>(PrinterSn.class);
        util.exportExcel(response, new ArrayList<PrinterSn>(), "打印机序列号模板");
    }


//导入数据

    @PostMapping("/import")
    public AjaxResult importData(@RequestParam("file") MultipartFile file, boolean updateSupport) {
        try {
            // 检查文件是否为空
            if (file.isEmpty()) {
                return AjaxResult.error("上传文件不能为空");
            }

            // 检查文件格式
            String filename = file.getOriginalFilename();
            if (filename == null || (!filename.endsWith(".xlsx") && !filename.endsWith(".xls"))) {
                return AjaxResult.error("请上传Excel文件");
            }

            // 使用ExcelUtil导入数据
            ExcelUtil<PrinterSn> util = new ExcelUtil<>(PrinterSn.class);
            List<PrinterSn> printerSnList = util.importExcel(file.getInputStream());

            if (printerSnList == null || printerSnList.isEmpty()) {
                return AjaxResult.error("导入数据为空或格式不正确");
            }

            // 调用服务层处理导入数据
            String message = printerSnService.importPrinterSn(printerSnList, updateSupport);
            return AjaxResult.success(message);

        } catch (Exception e) {
            logger.error("导入数据失败",e);
            String errorMsg = "导入失败: " + e.getMessage();
            errorMsg = errorMsg.replace("<br/>", "\n").replace("<[^>]*>", "");
            return AjaxResult.error(errorMsg);
//            return AjaxResult.error("导入失败: " + e.getMessage());
        }
    }
}
